﻿using System;
using System.Data;
using System.Text;
using TraditionalMalteseCuisine.App_Code.Functions;
using TraditionalMalteseCuisine.App_Code.Objects;
using TraditionalMalteseCuisine.App_Code.Operations;

namespace TraditionalMalteseCuisine
{
    public partial class MainMasterPage : System.Web.UI.MasterPage
    {
        #region Class Members
        private static int mMemberID = -1;
        #endregion


        #region Page_Load
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if ((!IsLoggedIn) && (MemberID != -1))
                {
                    SiteMember tempMember = Site_Functions.GetSiteMemberByID(MemberID);

                    if (tempMember != null)
                        SiteMember = Site_Functions.DoLogin(tempMember.Username, tempMember.Password);
                }
            }
        }
        #endregion


        #region Events
        protected void txtSearch_TextChanged(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("recipes.aspx?search={0}", txtSearch.Text));
        }
        #endregion


        #region Buttons
        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            SiteMember = null;

            MemberID = -1;

            Response.Cookies["TraditionalMalteseCuisine_MemberID"].Expires = DateTime.Now.AddDays(-1);

            Response.Redirect("default.aspx");
        }
        #endregion


        #region Methods
        protected void BuildMenuItems()
        {
            StringBuilder html_menuitems = new StringBuilder();

            DataTable dtParentCategories = ReadOp.GetDataTable(Tag.GetCommand_GetParentTags());

            if (dtParentCategories != null)
            {
                foreach (DataRow drParentCategory in dtParentCategories.Rows)
                {
                    html_menuitems.AppendFormat("<li><a href=\"#\" onclick=\"showChildMenus({0});\"><table style=\"width: 100%;\"><tr><td>&nbsp;</td><td class=\"middle_left\">{1}</td><td id=\"td_arrow_{0}\" class=\"down_arrow\"><div style=\"width: 100%; height: 100%;\">&nbsp;</div></td></tr></table></a></li>", drParentCategory[Tag.ID_TAG], drParentCategory[Tag.TAG_NAME_TAG]);

                    html_menuitems.AppendFormat("<li_hidden id=\"li_{0}\">", drParentCategory[Tag.ID_TAG]);

                    DataTable dtChildCategories = ReadOp.GetDataTable(Tag.GetCommand_GetChildTags((int)drParentCategory[Tag.ID_TAG]));
                    
                    if (dtChildCategories != null)
                    {
                        foreach (DataRow drChildCategory in dtChildCategories.Rows)
                        {
                            ////Too resource intensive to load the tags statistics on every page visit!
                            //int recipe_count = ReadOp.GetIntResult(Recipe.GetCommand_GetCountBy(Recipe.VIEW_NAME_ALL_RECIPES_DETAILS, RecipeTag.TAG_ID_TAG, drChildCategory[Tag.ID_TAG].ToString()));
                            //html_menuitems.AppendFormat("<a href=\"recipes.aspx?tag={0}\"><div style=\"height: 15px; padding-left: 20px; padding-top: 3px;\">{1} - ({2})</div></a>", drChildCategory[Tag.ID_TAG], drChildCategory[Tag.TAG_NAME_TAG], recipe_count);

                            html_menuitems.AppendFormat("<div id=\"li_hidden_item_tag\"><a href=\"recipes.aspx?tag={0}\"><div style=\"height: 15px; padding-left: 20px; padding-top: 3px;\">{1}</div></a></div>", drChildCategory[Tag.ID_TAG], drChildCategory[Tag.TAG_NAME_TAG]);
                        }
                    }
                    else
                    {
                        html_menuitems.Append("<div id=\"li_hidden_item_tag\" style=\"padding-left: 0; text-align: center;\">");
                        html_menuitems.Append("No Tags Here");
                        html_menuitems.Append("</div>");
                    }
                    html_menuitems.Append("</li_hidden>");
                }
            }

            Response.Write(html_menuitems.ToString());
        }

        protected void ListSubscriptions()
        {
            StringBuilder html_subscriptions = new StringBuilder();

            DataTable dtSubscribedWith = ReadOp.GetDataTable(Subscription.GetCommand_GetRecordsBy(Subscription.VIEW_NAME_ALL_SUBSCRIPTIONS_DETAILS, Subscription.MEMBER_ID_TAG, SiteMember.ID.ToString()));

            if ((dtSubscribedWith != null) && (dtSubscribedWith.Rows.Count > 0))
            {
                foreach (DataRow drSubscribedWith in dtSubscribedWith.Rows)
                {
                    html_subscriptions.AppendFormat("<div id=\"li_hidden_item\" onclick=\"document.location.href='profile.aspx?user={0}'\">", drSubscribedWith[Subscription.SUBSCRIBED_TO_MEMBER_ID_TAG]);
                    html_subscriptions.AppendFormat("<a href=\"rss.aspx?user={0}\" /><img src=\"SiteContentImages/Feed.png\" class=\"profile_thumbnail_image img_no_border\" style=\"float: right;\" /></a>", drSubscribedWith[Subscription.SUBSCRIBED_TO_MEMBER_ID_TAG]);
                    html_subscriptions.AppendFormat("<a href=\"profile.aspx?user={0}\">", drSubscribedWith[Subscription.SUBSCRIBED_TO_MEMBER_ID_TAG]);
                    html_subscriptions.AppendFormat("<span style=\"white-space: nowrap;\"><img src=\"{0}\" alt=\"Click to visit profile...\" class=\"profile_thumbnail_image img_no_border\" />{1} {2}</span>", drSubscribedWith[SiteMember.AVATAR_TAG], drSubscribedWith[SiteMember.FIRSTNAME_TAG], drSubscribedWith[SiteMember.LASTNAME_TAG]);
                    html_subscriptions.Append("</a>");
                    html_subscriptions.Append("</div>");
                }
            }
            else
            {
                html_subscriptions.Append("<div id=\"li_hidden_item\" style=\"padding-top: 15px; padding-left: 0; text-align: center;\">");
                html_subscriptions.Append("No Subscriptions");
                html_subscriptions.Append("</div>");
            }

            Response.Write(html_subscriptions.ToString());
        }

        protected string GetLatestNews()
        {
            string latestNews = ". . . no news to display yet . . .";

            DataTable dtNews = ReadOp.GetDataTable(SiteNews.GetCommand_GetLatestNews(1));

            if ((dtNews != null) && (dtNews.Rows.Count == 1))
                latestNews = dtNews.Rows[0][SiteNews.NEWS_TEXT_TAG].ToString();

            return latestNews;
        }
        #endregion


        #region Properties
        public SiteMember SiteMember
        {
            get { return ((Session["SiteMember"] == null) ? null : (SiteMember)Session["SiteMember"]); }
            set { Session["SiteMember"] = value; }
        }

        public bool IsLoggedIn
        {
            get { return (SiteMember != null); }
        }

        public static int MemberID
        {
            get { return mMemberID; }
            set { mMemberID = value; }
        }
        #endregion
    }
}